/**
* @CopyRight tstudy.com.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date  2015年4月3日 下午1:03:40
*/
package com.tstudy.laoshibang.profile;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.HttpManager;
import com.tstudy.laoshibang.base.WebViewFragment;
import com.tstudy.laoshibang.event.UpdateLoginStatusEvent;
import com.tstudy.laoshibang.event.UploadAvatarEvent;
import com.tstudy.laoshibang.login.LoginActivity;
import com.tstudy.laoshibang.utils.LogUtil;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.greenrobot.event.EventBus;
@EFragment(R.layout.profile)
public class ProfileFragment extends BaseFragment{
	final static String TAG = "profile";
	public ProfileFragment() {
		super();
	}
	
	@ViewById(R.id.profile_root)
	LinearLayout mRootLayout;
	
	@ViewById(R.id.profile_nick)
	TextView mNickTxt;
	
	@ViewById(R.id.profile_avatar)
	ImageView mAvatar;
	
	@ViewById(R.id.profile_login_status)
	TextView mLoginStatus;
	
	boolean mIsFirstLoad = true;
	Handler mHandler;
	LayoutInflater mInflater;
	

	public static void add(int containerViewId) {
		Bundle args = new Bundle();
		args.putInt(CONSTANT.ARGS.FRAGMENT_ID, containerViewId);
		Fragment fragment = ((BaseActivity)BaseApplication.mCurrentActivity).getFragmentByTag(TAG);
		if(fragment == null){
			fragment = Fragment.instantiate(BaseApplication.mContext,ProfileFragment_.class.getName(),args);
		}
		((BaseActivity)BaseApplication.mCurrentActivity).addInTab(containerViewId,fragment,TAG);	
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mHandler = new Handler();
		EventBus.getDefault().register(this);
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mInflater = inflater;
		super.onCreateView(inflater, container, savedInstanceState);
		LogUtil.d("--mBaseLayout==" + mRootLayout);
		mIsFirstLoad = mRootLayout == null;
		if (mRootLayout != null) {
			((ViewGroup) mRootLayout.getParent()).removeAllViews();
		}
		return mRootLayout;
	}
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	
	@AfterViews
	public void initView(){
//		if (mIsFirstLoad) {
			setView();
//		}
	}
	
	public void setView(){
		if (BaseApplication.checkLogin()) {
			mLoginStatus.setVisibility(View.GONE);
			mNickTxt.setText(BaseApplication.mCurrentUser.nick);
			HttpManager.getInstance().loadEditAvatar(mAvatar, BaseApplication.mCurrentUser.imgIdxName, R.drawable.profile_avatar_default);
			
		}else{
			mNickTxt.setText(R.string.profile_account);
			mLoginStatus.setText(R.string.profile_not_login);
			mLoginStatus.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
//					LoginFragment.add(mFragmentId, new LoginCallback() {
//						@Override
//						public void loginSuccess(boolean result) {
//							mLoginStatus.setVisibility(View.GONE);
//							BaseApplication.showToast(BaseApplication.mCurrentUser.nick);
//							mNickTxt.setText(BaseApplication.mCurrentUser.nick);
//							HttpManager.getInstance().loadEditAvatar(mAvatar, BaseApplication.mCurrentUser.imgIdxName, R.drawable.profile_avatar_default);
//						}
//					});
					LoginActivity.show();
				}
			});
		}
	}
	@Click({R.id.profile_avatar,R.id.profile_community,R.id.profile_account_layout,R.id.profile_invite_friend,R.id.profile_feedback,R.id.profile_setting,R.id.profile_app_store,R.id.profile_message})
	public void actionClick(View view){
		switch (view.getId()) {
		case R.id.profile_community:
			if (BaseApplication.checkLogin()) {
				MyCommunityFragment.add(mFragmentId,BaseApplication.mUserNo,null);
			}else{
//				LoginFragment.add(mFragmentId, new LoginCallback() {
//					@Override
//					public void loginSuccess(boolean result) {
//						if (result) {
//							setView();
//						}
//					}
//				});
				LoginActivity.show();
				
			}
			break;
		case R.id.profile_invite_friend:
			InviteFriendFragment.add(mFragmentId);
			break;
		case R.id.profile_account_layout:
			UserInfoFragment.add(mFragmentId);
			break;
		case R.id.profile_feedback:
			FeedbackFragment.add(mFragmentId);
			break;
		case R.id.profile_setting:
			SettingFragment.add(mFragmentId);
			break;
		case R.id.profile_app_store:
			WebViewFragment.add(mFragmentId,CONSTANT.APP_STORE_URL,BaseApplication.getResString(R.string.app_store_title));
			break;
		case R.id.profile_message:
			MessageListFragment.add(mFragmentId);
			break;
		case R.id.profile_avatar:
			if (BaseApplication.mCurrentUser != null) {
				if (!TextUtils.isEmpty(BaseApplication.mCurrentUser.imgIdxName)) {
					BigHeaderActivity.show(BaseApplication.mCurrentUser.imgIdxName);
				}
			}
			break;
		default:
			break;
		}
	}
	public void onEventMainThread(Object object) {
		if(object instanceof UploadAvatarEvent){
			setView();
		}else if(object instanceof UpdateLoginStatusEvent){
			setView();
		}
	}
	
	
	@Override
	public void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	};
	@Override
	public void clear() {
		// TODO Auto-generated method stub
	}

	@Override
	public void initArgs(Bundle args) {
		if (args != null) {
			mFragmentId = args.getInt(CONSTANT.ARGS.FRAGMENT_ID);
		}
	}

}
